import oss from 'ali-oss';

export interface PutOptions {

  /** After a file is uploaded successfully, the OSS sends a callback request to this URL. */
  url: string;

  /** The value of the request body when a callback is initiated, for example, key=$(key)&etag=$(etag)&my_var=$(x:my_var). */
  body: string;

  /** The host header value for initiating callback requests. */
  host?: string;

  /**
   * The Content-Type of the callback requests initiatiated,
   * It supports application/x-www-form-urlencoded and application/json,
   * and the former is the default value.
   */
  contentType?: string;

  /** Custom parameters are a map of key-values */
  customValue?: string;
}

export interface PutResponse {
  name: string;
  url: string;
  res: {
    status: number;
    statusCode: number;
    statusMessage: string;
    headers: Record<string, string>;
    size: number;
    aborted: boolean;
    rt: number;
    keepAliveSocket: boolean;
    data: Buffer;
    requestUrls: string[];
    timing: unknown;
    remoteAddress: string;
    remotePort: number;
    socketHandledRequests: number;
    socketHandledResponses: number;
  };
}

export interface OssStore {
  put(name: string, filepath: string, options?: PutOptions): Promise<PutResponse>;
}

const ossStore: OssStore = oss({
  accessKeyId: 'accessKeyId',
  accessKeySecret: 'accessKeySecret',
  bucket: 'bucket-name',
  // endpoint: 'oss-cn-guangzhou.aliyuncs.com',
  region: 'oss-cn-guangzhou',
});

export default ossStore;
